package com.chenbei.controller.admin.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenbei.common.annotation.OperationLog;
import com.chenbei.common.util.ApiResponseUtil;
import com.chenbei.common.util.PaginationUtil;
import com.chenbei.common.domain.vo.ApiResponse;
import com.chenbei.common.domain.vo.Pagination;
import com.chenbei.system.domain.SysDict;
import com.chenbei.system.domain.SysDictItem;
import com.chenbei.system.service.ISysDictItemService;
import com.chenbei.system.service.ISysDictService;
import com.chenbei.system.vo.req.SysDictItemReq;
import com.chenbei.system.vo.req.SysDictReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "字典管理")
@RestController
@RequestMapping("/admin/system/dict")
public class DictController {

    @Autowired
    private ISysDictService dictService;

    @Autowired
    private ISysDictItemService dictItemService;

    @SaCheckPermission(value = "system:dict:add", orRole = "root")
    @PostMapping("add")
    @Operation(summary = "新增字典")
    @OperationLog(moduleName = "字典管理", eventName = "新增字典")
    public ApiResponse<Object> add(@Valid @RequestBody SysDict sysDict) {
        dictService.add(sysDict);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:dict:delete", orRole = "root")
    @Operation(summary = "删除字典")
    @PostMapping("deleteById/{id}")
    @OperationLog(moduleName = "字典管理", eventName = "删除字典")
    public ApiResponse<Object> delete(@PathVariable Long id) {
        dictService.deleteById(id);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:dict:update", orRole = "root")
    @Operation(summary = "更新字典")
    @PostMapping("update")
    @OperationLog(moduleName = "字典管理", eventName = "更新字典")
    public ApiResponse<Object> update(@Valid @RequestBody SysDict sysDict) {
        dictService.update(sysDict);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:dict:list", orRole = "root")
    @Operation(summary = "字典分页列表")
    @GetMapping("listPage")
    public ApiResponse<Pagination<SysDict>> listPage(SysDictReq sysDictReq) {
        Page<SysDict> dictEntityPage = dictService.listPage(sysDictReq);
        return ApiResponseUtil.success(PaginationUtil.toPagination(dictEntityPage));
    }

    @SaCheckPermission(value = "system:dict_item:add", orRole = "root")
    @Operation(summary = "新增字典项")
    @OperationLog(moduleName = "字典管理", eventName = "新增字典项")
    @PostMapping("item/add")
    public ApiResponse<Object> itemAdd(@Valid @RequestBody SysDictItem sysDictItem) {
        dictItemService.add(sysDictItem);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:dict_item:delete", orRole = "root")
    @Operation(summary = "删除字典项")
    @OperationLog(moduleName = "字典管理", eventName = "删除字典项")
    @PostMapping("item/deleteById/{id}")
    public ApiResponse<Object> itemDelete(@PathVariable Long id) {
        dictItemService.deleteById(id);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:dict_item:update", orRole = "root")
    @Operation(summary = "更新字典项")
    @OperationLog(moduleName = "字典管理", eventName = "更新字典项")
    @PostMapping("item/update")
    public ApiResponse<Object> itemUpdate(@Valid @RequestBody SysDictItem sysDictItem) {
        dictItemService.update(sysDictItem);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:dict_item:list", orRole = "root")
    @Operation(summary = "字典项分页列表")
    @GetMapping("item/listPage")
    public ApiResponse<Pagination<SysDictItem>> itemListPage(SysDictItemReq sysDictItemReq) {
        Page<SysDictItem> dictItemEntityPage = dictItemService.listPage(sysDictItemReq);
        return ApiResponseUtil.success(PaginationUtil.toPagination(dictItemEntityPage));
    }
}
